
Ext.define('demo.controller.demoController', {
    extend: 'Ext.app.Controller',
       
    views: ['Viewport','menuTree','book','bookwin','presswin','userAdd','user','menuAccordion'],
    stores:['menuStore','bookStore','pressStore','userStore'],
    model:['menuModel','bookModel','pressModel','userModel'],
    
    init:function(){
    	this.control({
    		'menutree':{
    			select:this.selectPage
    		},
    		'db button[id=add]':{
    			click:this.bookAdd
    		},
    		'db button[id=delete]':{
    			click:this.bookDelete
    		},
    		'db':{
    			itemdblclick:this.editBook
    		},
    		'db button[id=edit]':{
    			click:this.btnEditBook
    		},
    		'db button[id=query]':{
    			click:this.queryBook
    		},
    		'dpress button[id=SavePress]':{
    			click:this.SavePress
    		},
    		'ua button[action=userAdd]':{
    			click:this.userAdd
    		},
    		'duser gridpanel':{
    			select: this.onGridpanelSelect
    		},
    		'duser button[action=addUserInfo]':{
    			click:this.onAddUserInfo
    		},
    		'duser button[action=saveUserInfo]':{
    			click:this.onSaveUserInfo
    		},
    		'duser button[action=editUserInfo]':{
    			click:this.onEditUserInfo
    		},
    		'duser button[action=deleteUserInfo]':{
    			click:this.onDeleteUserInfo
    		}
    	})
    },
    
    //树形菜单选择方法
    selectPage:function(view,record,index,eOpts){
    	
    	var ele = Ext.fly('app_tree');

    	
    	var title = record.get('text');
    	var leaf = record.get('leaf');
    	var tabPanel = Ext.getCmp('mainContent');
    	
    	if(false == leaf){
    		return;
    	}
    
		var newTab = tabPanel.getChildByElement(title);
		var tabPage;
    	if(null == newTab){
    		
    		if(title == '查看图书'){
	    		tabPage = tabPanel.add({
	    			title:title,
	    			layout:'fit',
	   				xtype:'db',
	    			closable:true,
	    		});
	    		
    		}
    		
    		if(title == '删除图书'){
	    			tabPage = tabPanel.add({
	    			title:title,
	    			layout:'fit',
	    			html:'江枫渔火对愁眠',
	    			closable:true,
	    		});

    		}
    		
    		if(title == '添加用户'){
	    			tabPage = tabPanel.add({
	    			title:title,
					xtype:'ua',
	    			closable:true,
	    		});
    		
    		}
    		
    		if(title == '查看用户'){
	    			tabPage = tabPanel.add({
	    			title:title,
					xtype:'duser',
	    			closable:true,
	    		});
  		
    		}
 
    	}
    	tabPanel.setActiveTab(tabPage);
    	
    },
    
    //图书添加事件
    bookAdd:function(btnObj){
    	var win = Ext.create('demo.view.bookwin');
    	win.show();
    	var store = btnObj.ownerCt.ownerCt.getStore();
    	win.down('button').addListener('click',function(btn){
    		var form = btn.ownerCt.ownerCt.down('form').getForm();
    		
    		var values = form.getValues();
    		
    		store.add({"BookNum":values["BookNum"],
    		"BookName":values["BookName"],
    		"BookPress":values["BookPress"],
    		"BookClassify":values["BookClassify"],
    		"BookPrice":values["BookPrice"] });
    		
    		win.hide();
    	});
    		
    },
    
    //图书删除事件
    bookDelete:function(btnObj){
    	var grid = btnObj.ownerCt.ownerCt;
    	var data = grid.getSelectionModel().getSelection();
    	
    	if(data.length == 0){
    		Ext.Msg.alert('提示','你没有选择数据！');
    		return;
    	}else{
    		var store = grid.getStore();
    		Ext.MessageBox.confirm('提示','确定要删除这'+data.length+'条数据？',function(value){
    			if(value == "yes"){
    				Ext.Array.each(data,function(record){
				 		store.remove(record);	
					});
    			}
    		});
    	}
    },
    
    //双击图书编辑事件
    editBook:function(view,record,e){
    	var win = Ext.create('demo.view.bookwin');
    	win.setTitle('图书信息编辑');
    	win.show();
    	win.down('form').loadRecord(record);
    	
    	win.down('button').addListener('click',function(btn){
    		var form = btn.up('window').down('form').getForm();
    		var record = form.getRecord();
    		record.set(form.getValues());
    		win.close();
    	})
    	
    },
    
    //按钮编辑事件
    btnEditBook:function(btnObj){
    	var grid = btnObj.ownerCt.ownerCt;
    	var data = grid.getSelectionModel().getSelection();
    	
    	if(data.length == 0){
    		Ext.Msg.alert('提示','你没有选择数据！');
    		return;
    	}
    	
    	if(data.length != 1){
    		Ext.Msg.alert('提示','只能选择一条数据进行编辑！');
    		return;
    	}
    	
    	var win = Ext.create('demo.view.bookwin');
	    win.setTitle('图书信息编辑');
	    win.show();
    	var form = win.down('form');
	    win.down('form').loadRecord(data[0]);
    	win.down('button').addListener('click',function(btn){
    		
	    	var record = form.getRecord();
	    	record.set(form.getValues());
    		win.close();
    	})
    	
    },
    
    //图书模糊查询
    queryBook:function(btnObj){
    	var keywords = Ext.getCmp('dim').getValue();
    	var store = btnObj.ownerCt.ownerCt.getStore();
    	var books = store.data.items;
    	
    	var newList = [];
		Ext.Array.each(books,function(record){
			if(record.data.BookName.indexOf(keywords) != -1){
				newList.push(record);
			}
		});
		
		store.loadData(newList);
    },

	//用户信息添加
	userAdd:function(btn){
		var values = btn.ownerCt.ownerCt.getValues();
		var data = Ext.JSON.encode(values);
		Ext.data.JsonP.request({
			url:'http://127.0.0.1:8080/SSM/userAdd',
			timeout:2000,
			params:{data:data},
			callbackKey: "jsonPCallback",
			success:function(result){
				console.info(result);
				alert("请求成功")
			},
			failure:function(result){
				alert("请求失败");
			}
		})
	},
	
	//用户表格选择事件
	onGridpanelSelect:function(rowmodel, record, index, eOpts){
		var form = Ext.getCmp('userInfo');
		form.loadRecord(record);
		var radio = form.down('radiogroup');
		if(record.data.sex == '男'){
			radio.items.get(0).setValue(true);
		}else{
			radio.items.get(1).setValue(true);
		}
		var editUser = Ext.getCmp('editUser');
		editUser.setDisabled(false);
		var deleteUser = Ext.getCmp('deleteUser');
		deleteUser.setDisabled(false);
	},
   
   //用户添加事件
   onAddUserInfo:function(btnObj,event,eOpts){
   	
   		
   		var form = Ext.getCmp('userInfo');
   		var fs = form.items.items;
   		Ext.Array.each(fs,function(item,index,allItems){
   				item.setReadOnly(false);
   			}
   		);
   		form.getForm().reset();
   		var saveUser = Ext.getCmp('saveUser');
   		saveUser.setDisabled(false);
   		
  },
  
    //用户信息保存事件
  	onSaveUserInfo:function(btnObj,event,eOpts){
  		var addUser = Ext.getCmp('addUser');
  		var store = btnObj.ownerCt.ownerCt.getStore();
  		var basic = Ext.getCmp('userInfo')
  		var form = basic.getForm();
  		var fs = basic.items.items;
  		if(addUser.disabled){
  			var record = form.getRecord();
    		record.set(form.getValues());
    		addUser.setDisabled(false);
    		Ext.Array.each(fs,function(item,index,allItems){
   				item.setReadOnly(true);
   			}
   		);
  		}else{	
  			var data = form.getValues();
  			store.add(data);
  			btnObj.setDisabled(false);
  			Ext.Array.each(fs,function(item,index,allItems){
   				item.setReadOnly(true);
   				}
   			);
  		}
  		
  	},
  	
  	//用户编辑数据
  	onEditUserInfo:function(btnObj,event,eOpts){
  		var form = btnObj.ownerCt.ownerCt.ownerCt.down('form');
  		var fs = form.items.items;
   		Ext.Array.each(fs,function(item,index,allItems){
   				item.setReadOnly(false);
   			}
   		);
   		//设置互斥的按钮禁用
   		var saveUser = Ext.getCmp('saveUser');
   		saveUser.setDisabled(false);
		var addUser = Ext.getCmp('addUser');
		addUser.setDisabled(true);
  	},
  	
  	//用户信息删除
  	onDeleteUserInfo:function(btnObj,event,eOpts){
  		var grid = btnObj.ownerCt.ownerCt;
  		var data = grid.getSelectionModel().getSelection()[0];
  		var store = grid.getStore();
  		var form = btnObj.ownerCt.ownerCt.ownerCt.down('form');
    	Ext.MessageBox.confirm('提示','确定要删除这条数据？',function(value){
    			if(value == "yes"){
    				Ext.Array.each(data,function(record){
				 		store.remove(data);					 		
   						form.getForm().reset(); 
   						var editUser = Ext.getCmp('editUser');
   						editUser.setDisabled(true);
   						var deleteUser = Ext.getCmp('deleteUser');
						deleteUser.setDisabled(true);
					});
    			}
           });
           
        
  	}
   
});